package com.api.base.service.impl;

import com.api.base.dao.SysRoleMapper;
import com.api.base.dao.SysRolePowerMapper;
import com.api.base.model.RoleMenu;
import com.api.base.model.SysRole;
import com.api.base.model.SysRoleAdd;
import com.api.base.model.SysRolePower;
import com.api.base.service.RoleMenuService;
import com.api.base.service.SysRoleService;
import com.api.core.service.AbstractService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import com.api.common.JSONUtils;

/**
 * Created by wanghuiwen on 2020/04/27.
 */
@Service
@Transactional
public class SysRoleServiceImpl extends AbstractService<SysRole> implements SysRoleService {
    @Resource
    private SysRoleMapper roleMapper;
    @Resource
    private SysRolePowerMapper sysRolePowerMapper;

    @Resource
    private RoleMenuService roleMenuService;

    @Override
    @Transactional
    public void addPower(String powers,Integer roleId) {
        roleMapper.deletePower(roleId);
        List<Integer> powerids = JSONUtils.json2WrapperList(powers,Integer.class);

        List<SysRolePower> rolePowers = new ArrayList<>();
        for (Integer pid: powerids) {
            SysRolePower rolePower = new SysRolePower();
            rolePower.setPowerId(pid);
            rolePower.setRoleId(roleId);
            rolePowers.add(rolePower);
        }
        if(rolePowers.size()>0)
            sysRolePowerMapper.insertListNoAuto(rolePowers);
    }

    @Override
    public void addRole(SysRoleAdd sysRoleAdd) {
        if(sysRoleAdd.getId()!=null){
            SysRole sysRole = this.findById(sysRoleAdd.getId());
            sysRole.setDescription(sysRoleAdd.getDescription());
            sysRole.setName(sysRoleAdd.getName());
            this.update(sysRole);

            List<RoleMenu> list = roleMenuService.getByRoleId(sysRoleAdd.getId());
            if(!list.isEmpty()){
                for(RoleMenu roleMenu : list){
                    roleMenuService.deleteById(roleMenu);
                }
            }
            String [] menuIds = sysRoleAdd.getMenus().split(",");
            for(int i=0;i<menuIds.length;i++){
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(sysRoleAdd.getId());
                roleMenu.setMenuId(Long.parseLong(menuIds[i]));
                roleMenuService.save(roleMenu);
            }
        }else{
            SysRole sysRole  = new SysRole();
            sysRole.setDescription(sysRoleAdd.getDescription());
            sysRole.setName(sysRoleAdd.getName());
            sysRole.setType(sysRoleAdd.getType());
            this.save(sysRole);
            String [] menuIds = sysRoleAdd.getMenus().split(",");
            for(int i=0;i<menuIds.length;i++){
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(sysRole.getId());
                roleMenu.setMenuId(Long.parseLong(menuIds[i]));
                roleMenuService.save(roleMenu);
            }
        }




    }
}
